package com.magist.manage.system.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.manage.system.dto.OffTagDto;
import com.magist.manage.system.entity.OffTag;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.magist.manage.system.entity.OffTag;
import com.magist.manage.system.dto.OffTagDto;
import com.magist.manage.system.service.OffTagService;

import java.util.List;

@Api(value = "RestOffTagController", description = "RestOffTagController接口")
@RestController
@RequestMapping("/rest/offTag")
public class RestOffTagController extends BaseController {

	@Autowired
	private OffTagService offTagService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("offTagDto") OffTagDto offTagDto) {
		OffTag offTag = new OffTag();
		BeanUtils.copyProperties(offTagDto,offTag);
		offTag.setId(offTagService.getNewId());
		offTagService.insert(offTag);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("offTagDto") OffTagDto offTagDto) {
		OffTag oldOffTag = offTagService.getById(offTagDto.getId());
		if(oldOffTag==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		OffTag offTag = new OffTag();
		BeanUtils.copyProperties(offTagDto,offTag);
		offTag.setModifyTime(oldOffTag.getModifyTime());
		if (offTagService.update(offTag)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("offTagDto") OffTagDto offTagDto) {
		ResultMessage result = ResultMessage.success();
		OffTag offTag = new OffTag();
		BeanUtils.copyProperties(offTagDto,offTag);
		String tagName = offTag.getTagName();
		String tagCode = offTag.getTagCode();
		if(!StringUtils.isEmpty(tagName)) {
			offTag.setTagName(tagName);
		}
		if(!StringUtils.isEmpty(tagCode)) {
			offTag.setTagCode(tagCode);
		}

		PageInfo<OffTag> pageInfo = offTagService.page(offTag,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		OffTag offTag = offTagService.getById(id);
		if(offTag==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(offTagService.delete(offTag)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		OffTag offTag = offTagService.getById(id);
		if(offTag==null)
			throw new BusinessException("无效的数据!");
		OffTagDto offTagDto = new OffTagDto();
		BeanUtils.copyProperties(offTag,offTagDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(offTagDto);
		return result;
	}

	@ApiOperation(value="列表", notes="所有标签")
	@RequestMapping(value = "tagList.do", method = RequestMethod.GET)
	public Object tagList(@ModelAttribute("offTagDto") OffTagDto offTagDto) {
		ResultMessage result = ResultMessage.success();
		OffTag offTag = new OffTag();
		BeanUtils.copyProperties(offTagDto,offTag);
		List<OffTag> offTags = offTagService.find(offTag);
		result.setContent(offTags);
		return result;
	}
}
